<?php
namespace app\common\dao\apps;

use app\common\dao\BaseDao;
use app\common\model\apps\AppsModel;

/**
 * Apps数据访问层
 */
class AppsDao extends BaseDao
{
    /**
     * 获取模型类名
     * @return string
     */
    protected function getModel(): string 
    {
        return AppsModel::class;
    }

    /**
     * 搜索构造器
     * @param array $where 查询条件
     * @return \think\db\Query
     */
    public function search(array $where = [])
    {
        $query = AppsModel::getDB()
            ->when(isset($where['id']) && !empty($where['id']), function ($query) use($where) {
                if (is_array($where['id'])) {
                    $query->whereIn('id', $where['id']);
                } else {
                    $query->where('id', $where['id']);
                }
            })
            ->when(isset($where['name']) && $where['name'] !== '', function ($query) use ($where) {
                $query->whereLike('name', '%' . trim($where['name']) . '%');
            })->when(isset($where['name_key']) && $where['name_key'] !== '', function ($query) use ($where) {
                $query->where('name', $where['name_key']);
            })->when(isset($where['appkey']) && $where['appkey'] !== '', function ($query) use ($where) {
                $query->whereLike('appkey', '%' . trim($where['appkey']) . '%');
            })->when(isset($where['appkey_key']) && $where['appkey_key'] !== '', function ($query) use ($where) {
                $query->where('appkey', $where['appkey_key']);
            })->when(isset($where['android_down_url']) && $where['android_down_url'] !== '', function ($query) use ($where) {
                $query->whereLike('android_down_url', '%' . trim($where['android_down_url']) . '%');
            })->when(isset($where['android_down_url_key']) && $where['android_down_url_key'] !== '', function ($query) use ($where) {
                $query->where('android_down_url', $where['android_down_url_key']);
            })->when(isset($where['android_scheme_url']) && $where['android_scheme_url'] !== '', function ($query) use ($where) {
                $query->whereLike('android_scheme_url', '%' . trim($where['android_scheme_url']) . '%');
            })->when(isset($where['android_scheme_url_key']) && $where['android_scheme_url_key'] !== '', function ($query) use ($where) {
                $query->where('android_scheme_url', $where['android_scheme_url_key']);
            })->when(isset($where['ios_down_url']) && $where['ios_down_url'] !== '', function ($query) use ($where) {
                $query->whereLike('ios_down_url', '%' . trim($where['ios_down_url']) . '%');
            })->when(isset($where['ios_down_url_key']) && $where['ios_down_url_key'] !== '', function ($query) use ($where) {
                $query->where('ios_down_url', $where['ios_down_url_key']);
            })->when(isset($where['ios_scheme_url']) && $where['ios_scheme_url'] !== '', function ($query) use ($where) {
                $query->whereLike('ios_scheme_url', '%' . trim($where['ios_scheme_url']) . '%');
            })->when(isset($where['ios_scheme_url_key']) && $where['ios_scheme_url_key'] !== '', function ($query) use ($where) {
                $query->where('ios_scheme_url', $where['ios_scheme_url_key']);
            })->when(isset($where['universal_links']) && $where['universal_links'] !== '', function ($query) use ($where) {
                $query->whereLike('universal_links', '%' . trim($where['universal_links']) . '%');
            })->when(isset($where['universal_links_key']) && $where['universal_links_key'] !== '', function ($query) use ($where) {
                $query->where('universal_links', $where['universal_links_key']);
            })->when(isset($where['package_name']) && $where['package_name'] !== '', function ($query) use ($where) {
                $query->whereLike('package_name', '%' . trim($where['package_name']) . '%');
            })->when(isset($where['package_name_key']) && $where['package_name_key'] !== '', function ($query) use ($where) {
                $query->where('package_name', $where['package_name_key']);
            })->when(isset($where['is_huawei']) && $where['is_huawei'] !== '', function ($query) use ($where) {
                $query->where('is_huawei', $where['is_huawei']);
            })->when(isset($where['is_xiaomi']) && $where['is_xiaomi'] !== '', function ($query) use ($where) {
                $query->where('is_xiaomi', $where['is_xiaomi']);
            })->when(isset($where['is_vivo']) && $where['is_vivo'] !== '', function ($query) use ($where) {
                $query->where('is_vivo', $where['is_vivo']);
            })->when(isset($where['is_oppo']) && $where['is_oppo'] !== '', function ($query) use ($where) {
                $query->where('is_oppo', $where['is_oppo']);
            })->when(isset($where['is_honour']) && $where['is_honour'] !== '', function ($query) use ($where) {
                $query->where('is_honour', $where['is_honour']);
            })->when(isset($where['is_yyb']) && $where['is_yyb'] !== '', function ($query) use ($where) {
                $query->where('is_yyb', $where['is_yyb']);
            })->when(isset($where['icon']) && $where['icon'] !== '', function ($query) use ($where) {
                $query->whereLike('icon', '%' . trim($where['icon']) . '%');
            })->when(isset($where['icon_key']) && $where['icon_key'] !== '', function ($query) use ($where) {
                $query->where('icon', $where['icon_key']);
            })->when(isset($where['banner']) && $where['banner'] !== '', function ($query) use ($where) {
                $query->whereLike('banner', '%' . trim($where['banner']) . '%');
            })->when(isset($where['banner_key']) && $where['banner_key'] !== '', function ($query) use ($where) {
                $query->where('banner', $where['banner_key']);
            })->when(isset($where['user_id']) && $where['user_id'] !== '', function ($query) use ($where) {
                $query->where('user_id', $where['user_id']);
            })
            ->when(isset($where['is_del']) && $where['is_del'] !== '', function ($query) use($where) {
                $query->where('is_del', $where['is_del']);
            })
            ->when(isset($where['status']) && $where['status'] !== '', function ($query) use($where) {
                $query->where('status', $where['status']);
            });

        return $query;
    }
}
